package cn.kailangtu.dataspot.total;

import org.litepal.LitePal;

import java.util.List;

import cn.kailangtu.dataspot.db.NewTotalDataEntity;

public class TotalDataHistory {

    int count = 600;

    private int[] avgVoltage = new int[count];
    private int[] avgElectric = new int[count];
    private int[] voltageA = new int[count];
    private int[] voltageB = new int[count];
    private int[] voltageC = new int[count];
    private int[] electricA = new int[count];
    private int[] electricB = new int[count];
    private int[] electricC = new int[count];

    private static TotalDataHistory instance = null;

    public synchronized static TotalDataHistory getInstance()
    {
        if(instance == null)
        {
            instance = new TotalDataHistory();
        }
        return instance;
    }

    public int[] getAvgVoltage() {
        return avgVoltage;
    }

    public void setAvgVoltage(int[] avgVoltage) {
        this.avgVoltage = avgVoltage;
    }

    public int[] getAvgElectric() {
        return avgElectric;
    }

    public void setAvgElectric(int[] avgElectric) {
        this.avgElectric = avgElectric;
    }

    public int[] getVoltageA() {
        return voltageA;
    }

    public void setVoltageA(int[] voltageA) {
        this.voltageA = voltageA;
    }

    public int[] getVoltageB() {
        return voltageB;
    }

    public void setVoltageB(int[] voltageB) {
        this.voltageB = voltageB;
    }

    public int[] getVoltageC() {
        return voltageC;
    }

    public void setVoltageC(int[] voltageC) {
        this.voltageC = voltageC;
    }

    public int[] getElectricA() {
        return electricA;
    }

    public void setElectricA(int[] electricA) {
        this.electricA = electricA;
    }

    public int[] getElectricB() {
        return electricB;
    }

    public void setElectricB(int[] electricB) {
        this.electricB = electricB;
    }

    public int[] getElectricC() {
        return electricC;
    }

    public void setElectricC(int[] electricC) {
        this.electricC = electricC;
    }


    public void find(long startTime)
    {

        long endTime = startTime + (count+1) * 1000;

        List<NewTotalDataEntity> ls =   LitePal.where("createTime >= ？AND createTime < ?",String.valueOf(startTime),String.valueOf(endTime)).limit(count).find(NewTotalDataEntity.class);
        if(ls != null && ls.size()>0)
        {
            int index = 0;
            for(int i = 599;i>=0;i--)
            {
                if(ls.size()> index)
                {
                    NewTotalDataEntity newTotalDataEntity = ls.get(index);
                    int voltageACurrent = 0;
                    int voltageBCurrent = 0;
                    int voltageCCurrent = 0;
                    int avgVoltageCurrent = 0;
                    int electricACurrent = 0;
                    int electricBCurrent = 0;
                    int electricCCurrent = 0;
                    int avgElectricCurrent = 0;

                    voltageACurrent = newTotalDataEntity.getVoltageA();
                    voltageBCurrent = newTotalDataEntity.getVoltageB();
                    voltageCCurrent = newTotalDataEntity.getVoltageC();
                    avgVoltageCurrent = (voltageACurrent + voltageBCurrent + voltageCCurrent)  / 3;
                    electricACurrent = newTotalDataEntity.getElectricA();
                    electricBCurrent = newTotalDataEntity.getElectricB();
                    electricCCurrent = newTotalDataEntity.getElectricC();
                    avgElectricCurrent = (electricACurrent+electricBCurrent+electricCCurrent) /3;

                    voltageA[i] = voltageACurrent;
                    voltageB[i] = voltageBCurrent;
                    voltageC[i] = voltageCCurrent;
                    avgVoltage[i] = avgVoltageCurrent;
                    electricA[i] = electricACurrent;
                    electricB[i] = electricBCurrent;
                    electricC[i] = electricCCurrent;
                    avgElectric[i] = avgElectricCurrent;
                }
                else
                {
                    voltageA[i] = 0;
                    voltageB[i] = 0;
                    voltageC[i] = 0;
                    avgVoltage[i] = 0;
                    electricA[i] = 0;
                    electricB[i] = 0;
                    electricC[i] = 0;
                    avgElectric[i] = 0;
                }
            }
        }
    }
}
